package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import tools.CheckboxUser;
import tools.OptionReader;
import calendar.Calendar;

/**
 * Klasse repräsentiert das Hinzufügen von Kalendern
 * 
 * @author Michi
 * 
 */
public class AddtoMyCals extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1872348462455994967L;
	private OptionReader or;
	private String[] windowColorsSeperated;
	private int r;
	private int g;
	private int b;
	@SuppressWarnings("unused")
	private DefaultListModel dlm;
	@SuppressWarnings("unused")
	private CheckBoxList users;
	private Connection connection = Test.getConnection();
	@SuppressWarnings("unused")
	private Statement stat;
	@SuppressWarnings("unused")
	private LinkedList<CheckboxUser> llUser = new LinkedList<CheckboxUser>();

	// Sprache
	int speak = 1;
	String[] fenstername = { "Kalender Hinzuf\u00FCgen", "Add Calendar",
			"Calendarium Addere" };
	String[] buttonadd = { "Hinzuf\u00FCgen", "Add", "Addicere" };
	String[] buttoncancel = { "Abbrechen", "Cancel", "Destruere" };
	String[] textname = { "Sie wurden zu dem Kalender",
			"You have been added to:", "Erant autem in calendar" };
	String[] textname2 = { "hinzugefügt, möchten Sie annehmen?",
			"Do you want to accept?", "addidit, formam sumpseris vos volo" };
	String[] einladen = { "Zu Kalender einladen:", "Invite to calendar:",
			"Duis Calendar:" };
	@SuppressWarnings("unused")
	private JTextField textField;

	/**
	 * Konstruktor
	 * 
	 * @param toadd
	 *            Kalender der hinzugefügt werden soll
	 * @param openedfrom
	 *            Kalender der hinzugefügt wird
	 */
	public AddtoMyCals(final Calendar toadd, final Object openedfrom) {

		or = new OptionReader();
		try {
			or.getOpionsFromFile();
		} catch (IOException e2) {

			e2.printStackTrace();
		}

		String windowColors = or.getColorSetting();
		windowColorsSeperated = windowColors.split(",");
		r = Integer.parseInt(windowColorsSeperated[0]);
		g = Integer.parseInt(windowColorsSeperated[1]);
		b = Integer.parseInt(windowColorsSeperated[2]);
		speak = Integer.parseInt(or.getLanguage());

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle(fenstername[speak]);

		Dimension size = new Dimension(650, 150);
		setSize(size);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);

		setBackground(new Color(r, g, b));
		getContentPane().setLayout(null);

		JLabel name = new JLabel();
		name.setBounds(20, 24, 600, 28);
		name.setText(textname[speak] + " " + toadd.getName() + " "
				+ textname2[speak]);
		getContentPane().add(name);

		JButton btnHinzufgen = new JButton(buttonadd[speak]);
		btnHinzufgen.setBounds(470, 80, 106, 29);
		getContentPane().add(btnHinzufgen);

		ActionListener addbtn = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					Statement stat = connection.createStatement();
					stat.executeQuery("use `sopra-ws12-awesome_fireballs`;");

					PreparedStatement ppst = connection
							.prepareStatement("UPDATE `sopra-ws12-awesome_fireballs`.`Calendar` SET `accepted`=? WHERE `idCalendar`=? and`idUser`=?;");
					connection.setAutoCommit(false);

					ppst.setInt(1, 1);
					ppst.setInt(2, toadd.getIdCalendar());
					ppst.setInt(3, Test.iduser);
					ppst.addBatch();
					ppst.executeBatch();

					connection.setAutoCommit(true);

				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null,
							"Es konnte keine Verbindung zur Datenbank hergestellt werden",
							"Haaalt! Stop!", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
				Object kal = Test.getMainInstance().getKalender();
				if(kal instanceof CalView){
					((CalView) kal).setEs();
					((CalView) kal).setTs();
				}
				if (kal instanceof WeekView){
					((WeekView) kal).setEs();
					((WeekView) kal).setEs();
				}
				((Test) openedfrom).updateLeftPane(true);
				((Test) openedfrom).updateCalendar();
				

				dispose();

			}
		};

		btnHinzufgen.addActionListener(addbtn);

		JButton btnAbbrechen = new JButton(buttoncancel[speak]);
		btnAbbrechen.setBounds(361, 80, 106, 29);
		getContentPane().add(btnAbbrechen);

		ActionListener cancle = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Statement stat = connection.createStatement();
					stat.executeQuery("use `sopra-ws12-awesome_fireballs`;");

					PreparedStatement ppst = connection
							.prepareStatement("DELETE FROM `sopra-ws12-awesome_fireballs`.`Calendar` WHERE `idCalendar`= ? and`idUser`= ?;");
					connection.setAutoCommit(false);

					ppst.setInt(1, toadd.getIdCalendar());
					ppst.setInt(2, Test.iduser);
					ppst.addBatch();
					ppst.executeBatch();

					connection.setAutoCommit(true);

				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(null,
							"Es konnte keine Verbindung zur Datenbank hergestellt werden",
							"Haaalt! Stop!", JOptionPane.ERROR_MESSAGE);
					e1.printStackTrace();
				}
				dispose();
			}
		};

		btnAbbrechen.addActionListener(cancle);
	}
}
